// 数据导入模块
const state = {
  // 导入的数据
  //[{"id":1,"sourceCity":"铜陵市","targetCity":"合肥市","sourceLng":117.816576,"sourceLat":30.929935,"targetLng":117.283042,"targetLat":31.86119,}]
  importedData: [],

  // 导入状态
  importStatus: {
    show: false,
    type: '', // 'loading', 'success', 'error'
    message: ''
  },

  // 导入进度
  importProgress: {
    show: false,
    percentage: 0
  },

  // 导入历史记录
  importHistory: [],

  // 最后导入时间
  lastImportTime: null
}

const getters = {
  getImportedData: state => state.importedData,
  getImportStatus: state => state.importStatus,
  getImportProgress: state => state.importProgress,
  getImportHistory: state => state.importHistory,
  getLastImportTime: state => state.lastImportTime,
  hasImportedData: state => state.importedData.length > 0,
  getImportedDataCount: state => state.importedData.length
}

const mutations = {
  // 设置导入数据
  SET_IMPORTED_DATA(state, data) {
    state.importedData = data
    state.lastImportTime = new Date().toISOString()
  },

  // 添加导入数据（追加模式）
  ADD_IMPORTED_DATA(state, data) {
    state.importedData = [...state.importedData, ...data]
    state.lastImportTime = new Date().toISOString()
  },

  // 清除导入数据
  CLEAR_IMPORTED_DATA(state) {
    state.importedData = []
    state.lastImportTime = null
  },

  // 设置导入状态
  SET_IMPORT_STATUS(state, status) {
    state.importStatus = { ...state.importStatus, ...status }
  },

  // 设置导入进度
  SET_IMPORT_PROGRESS(state, progress) {
    state.importProgress = { ...state.importProgress, ...progress }
  },

  // 添加导入历史记录
  ADD_IMPORT_HISTORY(state, record) {
    const historyRecord = {
      id: Date.now(),
      timestamp: new Date().toISOString(),
      ...record
    }

    state.importHistory.unshift(historyRecord)

    // 只保留最近的10条记录
    if (state.importHistory.length > 10) {
      state.importHistory = state.importHistory.slice(0, 10)
    }
  },

  // 清除导入历史
  CLEAR_IMPORT_HISTORY(state) {
    state.importHistory = []
  },

}

const actions = {
  // 初始化模块
  initializeImportData() {
    console.log('数据导入模块初始化完成')
  },

  // 导入数据
  async importData({ commit }, data) {
    try {
      commit('SET_IMPORTED_DATA', data)

      // 添加到历史记录
      commit('ADD_IMPORT_HISTORY', {
        type: 'import',
        count: data.length,
        success: true
      })

      return { success: true, data }
    } catch (error) {
      console.error('导入数据失败:', error)
      commit('ADD_IMPORT_HISTORY', {
        type: 'import',
        success: false,
        error: error.message
      })
      throw error
    }
  },

  // 追加数据
  async appendData({ commit }, data) {
    try {
      commit('ADD_IMPORTED_DATA', data)

      // 添加到历史记录
      commit('ADD_IMPORT_HISTORY', {
        type: 'append',
        count: data.length,
        success: true
      })

      return { success: true, data }
    } catch (error) {
      console.error('追加数据失败:', error)
      commit('ADD_IMPORT_HISTORY', {
        type: 'append',
        success: false,
        error: error.message
      })
      throw error
    }
  },

  // 清除所有数据
  clearAllData({ commit }) {
    commit('CLEAR_IMPORTED_DATA')
    commit('CLEAR_IMPORT_HISTORY')
    commit('SET_IMPORT_STATUS', { show: false })
    commit('SET_IMPORT_PROGRESS', { show: false, percentage: 0 })
  },

  // 设置导入状态
  setImportStatus({ commit }, status) {
    commit('SET_IMPORT_STATUS', status)
  },

  // 设置导入进度
  setImportProgress({ commit }, progress) {
    commit('SET_IMPORT_PROGRESS', progress)
  },

  // 隐藏导入状态
  hideImportStatus({ commit }) {
    commit('SET_IMPORT_STATUS', { show: false, type: '', message: '' })
  },

  // 隐藏导入进度
  hideImportProgress({ commit }) {
    commit('SET_IMPORT_PROGRESS', { show: false, percentage: 0 })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
